package com.example.test;

import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,OnItemSelectedListener {
	private List<VocabularyDto> mListVocal=null;
	private VocabularyDto mCurrentVocal=null;
	private TextView mTvNew;
	private EditText mEdtNew;
	private TextView mTvReview;
	private TextView mTvResult;
	private Button mBtnNext;
	private Button mBtnReview;
	private Button mBtnChange;
	private Spinner spinnerFrom, spinnerTo;
	private int mFrom=0;
	private int mTo=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mTvNew=(TextView) findViewById(R.id.txtNew);
		mEdtNew=(EditText) findViewById(R.id.edtText);
		mTvReview=(TextView) findViewById(R.id.txtReview);
		mTvResult=(TextView) findViewById(R.id.txtResult);
		mBtnNext=(Button) findViewById(R.id.btnNext);
		mBtnReview=(Button) findViewById(R.id.btnReview);
		spinnerFrom = (Spinner) findViewById(R.id.fromSpinner);
		spinnerTo = (Spinner) findViewById(R.id.toSpinner);
		mBtnNext.setOnClickListener(this);
		mBtnReview.setOnClickListener(this);
		spinnerFrom.setOnItemSelectedListener(this );
		spinnerTo.setOnItemSelectedListener(this);
		new VocabularyTask(this,1,1).execute();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_settings:
			Intent intent = new Intent(MainActivity.this, ShowVocabulary.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
		
	}
	private class VocabularyTask extends AsyncTask<String, String , Integer>{
		Context mContext;
		private int mStart =0;
		private int mEnd=0;
		public VocabularyTask(Context context,int start,int end ){
			mContext=context;
			mStart=start;
			mEnd = end;
			mListVocal =null;
			mCurrentVocal = null;
		}
		@Override
		protected Integer doInBackground(String... params) {
			VocabularyDao vocal=new VocabularyDao(mContext);
			
			
			mListVocal =vocal.getListVocabulary(mStart,mEnd);
			return 0;
		}
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if(mListVocal!=null && mListVocal.size()>0){
				mCurrentVocal=getRanDom();
				mTvNew.setText(mCurrentVocal.vn_stran);
				
			}
			else{
				mTvReview.setText("");
				mEdtNew.setText("");
				mTvNew.setText("");
			}
		}
	}
	private VocabularyDto getRanDom(){
		if(mListVocal!=null && mListVocal.size()>0){
			int min = 1;
			int max = mListVocal.size();
			Random r = new Random();
			int i1 = r.nextInt(max - min + 1) + min;
			return mListVocal.get(i1-1);
		}
		else
		{
			return null;
		}
	}
	private boolean CompareString(String str1,String str2){
		if(str1.trim()!="" && str2.trim()!=""){
			if(str1.trim().equals(str2.trim())){
				return true;
			}
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if(mCurrentVocal!=null){
			switch (v.getId()) {
			case R.id.btnNext:
				if(CompareString(mCurrentVocal.jp_stran,mEdtNew.getText().toString())){
					mTvResult.setText("Pro quá! ");
				}
				else
				{
					mTvResult.setText("Gà quá! ");
				}
				mCurrentVocal=getRanDom();
				mTvNew.setText(mCurrentVocal.vn_stran);
				mTvReview.setText("");
				mEdtNew.setText("");
				break;
			case R.id.btnReview:
				mTvReview.setText(mCurrentVocal.jp_stran);
				mTvResult.setText("Tiếp tục đi ku ! ");
				break;
			default:
				break;
			}
		}
		else{
			Toast.makeText(this, "Hiện khong có dữ !", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		switch (arg0.getId()) {
		case R.id.fromSpinner:

				mFrom = arg2+1;	
				System.out.println("==============bo tay============"+mFrom + ""+mTo);
				new VocabularyTask(this,mFrom,mTo).execute();

			break;
		case R.id.toSpinner:

				mTo =arg2+1;	
				System.out.println("==============bo tay============"+mFrom + ""+mTo);
				new VocabularyTask(this,mFrom,mTo).execute();
		

			break;
		default:
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}
}
